﻿package com.monboo.utils{
	/**
	 * @author robin for cat-Album 2010-03-26
	 * @RollPage 翻页类
	 * $vars:Object可以设置以下属性
	 * 		pageNum:每页子对象数
	 * 		total:总子对象数
	 * 		childWidth:子对象宽
	 * 		childHeight:子对象高
	 * 		childMargin:子对象边距
	 * @email:rman@live.cn
	 * @blog:www.xncat.com
	 */
	
	import com.gs.TweenLite;
	
	import flash.display.Sprite;

	public class RollPage {
		private var pageMc:Sprite;
		private var vars:Object;
		private var aix:String;
		private var _page:uint=1;

		private var numChildren:uint;
		private var pageNum:uint;
		private var spanLength:uint;
		private var childWidth:uint;
		private var childHeight:uint;
		private var childMargin:uint;
		private var totalPage:uint;
		private var rollMask:Sprite;
		
		public function RollPage() {
			rollMask = new Sprite();
			rollMask.name = "rollMask";
		}
		public function setRoll($pageMc:Sprite,$vars:Object,$aix:String="x"):void{
			pageMc = $pageMc;
			vars = $vars;
			aix = $aix;
			
			numChildren = $vars["totalNum"];
			pageNum = $vars["pageNum"];
			
			childMargin = $vars["childMargin"];
			childWidth = $vars["childWidth"];
			childHeight = $vars["childHeight"];
			
			totalPage = Math.ceil(numChildren/pageNum)-1;
			
			if (aix=="x") {
				spanLength = (childWidth+childMargin)*pageNum-childMargin;
				setArea(spanLength,childHeight);
			} else if (aix == "y") {
				spanLength = (childHeight+childMargin)*pageNum-childMargin;
				setArea(childWidth,spanLength);
			}
			
			var pageParent:Sprite=pageMc.parent as Sprite;
			if(pageParent.getChildByName("mask") == null){
				pageParent.addChild(rollMask);
			}
			
			pageMc.mask=rollMask;
		}
		public function toRoll($page:uint):void {

			if ($page<=0) {
				trace("firstpage");
			} else if ($page > Math.ceil(numChildren/pageNum)) {
				trace("endpage");
			} else {
				var move:Number;
				if (aix=="x") {	
					if($page == Math.ceil(numChildren/pageNum)){
						move = ($page-1)*(spanLength+childMargin)-(childWidth+childMargin)*(pageNum-lastPageNum(totalPage));
						TweenLite.to(pageMc,.5,{x:-move});
					}else{
						move = ($page-1)*(spanLength+childMargin);
						TweenLite.to(pageMc,.5,{x:-move});
					}
					
				} else if (aix == "y") {
					if($page == Math.ceil(numChildren/pageNum)){
						move = ($page-1)*(spanLength+childMargin)-(childHeight+childMargin)*(pageNum-lastPageNum(totalPage));
						TweenLite.to(pageMc,.5,{y:-move});
					}else{
						move = ($page-1)*(spanLength+childMargin);
						TweenLite.to(pageMc,.5,{y:-move});
					}
				}
				_page=$page;
			}

		}


		public function set page($page:uint):void {
			_page=$page;
		}
		public function get page():uint {
			return _page;
		}
		private function setArea(w:uint,h:uint,alpha:Number= 0.5):void {
			rollMask.alpha=alpha;
			rollMask.graphics.beginFill(0x000000,1);
			rollMask.graphics.drawRect(0,0,w,h);
			rollMask.graphics.endFill();
		}
		private function lastPageNum(n:Number):Number{
			var k:Number;
			if(numChildren>pageNum){
				if(n == Math.ceil(numChildren/pageNum)-1){
					k = numChildren%pageNum;
					if(k ==0){
						k = pageNum;
					}	
				}else{
					k= pageNum;
				}
			}else{
				k =numChildren;
			}
			return k;
		}
	}
}